<?php
/* explain:
 * Project:     
 * File:        Job.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
include_once("CouponDomain.php");
include_once("CouponCustomer.php");

class CouponImpl extends Common
{
	public $conHandle;
	public $errorInfo;
	
	private $iUserID;
	
	private $objLog;
	
	function CouponImpl($DBTag, $userid)
	{
		parent::Common();
		$this->conHandle 	= $this->getConnection($DBTag);
		
		$this->objLog 		= new Logs($userid);
		$this->iUserID 		= $userid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getByID($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM coupon WHERE id = $id";
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* 
	 *
	 * @param  string 	strOrders	
	 * @param  string  	name 	
	 * @param  string 	namecn 
	 * @return boolean 	true or fail
	 * 
	 */
	public function add($arrPopedom = array(), $name, $summary)
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());

		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
			
		$sql = "INSERT INTO coupon SET createdTime = UTC_TIMESTAMP(), modifiedTime = UTC_TIMESTAMP(), name = '$name', summary='" . addslashes($summary) . "'";
		$rs = mysql_query($sql, $this->conHandle) or die(mysql_error());  
		return $rs ? true: false;
	}
		
	/* modify menu
	 *
	 * @param  string 	id			modify menu id
	 * @param  string 	strOrders	
	 * @param  string  	name 	
	 * @param  string 	summary 
	 * @return boolean 	true or fail
	 */
	public function edit($arrPopedom = array(), $id, $name, $summary)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "id=$id");
			
		$sql = "UPDATE coupon SET summary='" . addslashes($summary) . "', name = '$name' WHERE id=$id";
		$rs = mysql_query($sql, $this->conHandle) or die ($sql . mysql_error($this->conHandle));  
		return $rs ? true: false;
	}
			
	/* delete menu
	 *
	 * @param  int    	id
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
			
		$sql = "DELETE FROM coupon WHERE id = $id";
		$rs	 = mysql_query($sql, $this->conHandle);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* list menu
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $condition, $page = 1, $discount = 20, $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = "SELECT count(id) FROM coupon$condition";

		$rs  = mysql_query($sql, $this->conHandle);
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT * FROM coupon$condition";
		else{
			$sql = "SELECT * FROM coupon$condition LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	/* list all custom condition data
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrBack = array();
		$sql = "SELECT * FROM coupon$condition";
        $rs  = mysql_query($sql, $this->conHandle);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}
	
	public function getCoupon($DBtag, $code)
	{
		$isreturn 	= false;
		$conn 		= null;
		
		try{
			$sql = "SELECT id, scope, scopeID, type, offValue, isBinding, usedLimit, usedCount, expireTime FROM coupon WHERE code = '$code'";
			
			$conn 	= $this->getConnection($DBtag);
			$rs  	= mysql_query($sql, $conn) or die("Invalid query: " . $sql . mysql_error($conn));

			if($arrData = mysql_fetch_assoc($rs)){
				$coupon = new CouponDomain();
				
				$coupon->setID($arrData['id']);
				$coupon->setScope($arrData['scope']);
				$coupon->setScopeID($arrData['scopeID']);
				$coupon->setType($arrData['type']);
				$coupon->setOffValue($arrData['offValue']);
				
				$coupon->setIsBinding($arrData['isBinding']);
				
				$coupon->setUsedLimit($arrData['usedLimit']);
				$coupon->setUsedCount($arrData['usedCount']);
				
				$coupon->setExpireTime($arrData['expireTime']);
				
				$coupon->setCode($code);
				
				if($arrData['isBinding'] == 'YES'){
					$sql	= "SELECT id, customerID, type, couponID FROM customer_coupon WHERE couponID = " . $arrData['id'];
					$rs  	= mysql_query($sql, $conn) or die("Invalid query: " . $sql . mysql_error($conn));

					if($arrData = mysql_fetch_row($rs)){
						$couponCustomer = new CouponCustomer();
						
						$couponCustomer->setID($arrData['id']);
						$couponCustomer->setType($arrData['type']);
						$couponCustomer->setCustomerID($arrData['customerID']);
						$couponCustomer->setCouponID($arrData['couponID']);
						
						$coupon->setCouponCustomer($couponCustomer);
					}else
						$coupon->setCouponCustomer(false);
				}
				
				return $coupon;
			}
		}catch(Exception $e){
			echo mysql_error($conn) . $sql;
		}
		
		if($conn != null)
			$this->freeConnection($DBtag, $conn);
			
		return $isreturn;
	}
	
	
}
?>
